﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entity.Car_Rental;
using Entity.CommonEnums;
using System.Transactions;

namespace Data.Car_Rental
{
    public class ProductRepository : Car_RentalRepositoryBase, IProductRepository
    {
        #region IProductRepository 成员

        public Entity.VMessage Car_Rental(Entity.Car_Rental.Product entity)
        {
            throw new NotImplementedException();
        }

        public Entity.PagedList<Entity.Car_Rental.Product> GetProduct(Entity.VPredication vp, Entity.PagingParam pp)
        {
            var linq = from i in this.GetModelForSite()
                       join j in new Product_Property_RRepository().GetDetailModel() on i.ProductID equals j.ProductID into list
                       select new Product_Ext
                       {
                           CategoryID = i.CategoryID,
                           CreateDate = i.CreateDate,
                           Discount = i.Discount,
                           Image = i.Image,
                           Info = i.Info,
                           MarketPrice = i.MarketPrice,
                           ProductCount = i.ProductCount,
                           ProductID = i.ProductID,
                           ProductName = i.ProductName,
                           RealPrice = i.RealPrice,
                           Status = i.Status,
                           UpdateDate = i.UpdateDate,
                           SmallImage = i.SmallImage,
                           PropertyInfo = i.PropertyInfo,
                           Product_Property_R_Extend = list,
                       };
            return new Entity.PagedList<Product>(linq, pp.PageIndex, pp.PageSize);
        }

        public IQueryable<Entity.Car_Rental.Product> GetModelForSite()
        {
            return GetModelForBackground().Where(i => i.Status != (int)Status.Deleted);
        }

        public IQueryable<Entity.Car_Rental.Product> GetModelForBackground()
        {
            return this.GetModel();
        }

        public IQueryable<Product> GetDetailModel()
        {
            var linq = from data in this.GetModel()
                       join data2 in new Product_Property_RRepository().GetDetailModel() on data.ProductID equals data2.ProductID into list
                       select new Product_Ext
                         {
                             CategoryID = data.CategoryID,
                             CreateDate = data.CreateDate,
                             Discount = data.Discount,
                             Image = data.Image,
                             Info = data.Info,
                             MarketPrice = data.MarketPrice,
                             ProductCount = data.ProductCount,
                             ProductID = data.ProductID,
                             ProductName = data.ProductName,
                             RealPrice = data.RealPrice,
                             Status = data.Status,
                             UpdateDate = data.UpdateDate,
                             SmallImage = data.SmallImage,
                             PropertyInfo = data.PropertyInfo,
                             Product_Property_R_Extend = list,
                         };
            return linq;
        }

        public void UpdateProductCount(long productID, int count)
        {
            DB.ExecuteCommand("update product set ProductCount=ProductCount+{0} where productid={1}", count, productID);
        }
        #endregion

        #region IRepository<Product> 成员

        public void Update(Product entity)
        {
            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    base.UpdateEntity(entity);
                    if (entity.Product_Property_R_Extend != null && entity.Product_Property_R_Extend.Any())
                    {
                        entity.Product_Property_R_Extend.ToList().ForEach(i => i.ProductID = entity.ProductID);
                        new Product_Property_RRepository().Delete(new Product_Property_RRepository().GetModel().Where(i => i.ProductID == entity.ProductID).ToList());
                        new Product_Property_RRepository().Insert(entity.Product_Property_R_Extend.ToList());
                    }
                    trans.Complete();
                }
                catch (Exception)
                {

                    throw;
                }
                finally
                {

                }
            }

        }

        public void Update(IList<Product> list)
        {
            base.UpdateEntity(list.Cast<Entity.EntityBase>().ToList());
        }

        public void Insert(Product entity)
        {
            using (TransactionScope trans = new TransactionScope())
            {
                try
                {
                    base.InsertEntity(entity);
                    if (entity.Product_Property_R_Extend != null && entity.Product_Property_R_Extend.Any())
                    {
                        entity.Product_Property_R_Extend.ToList().ForEach(i => i.ProductID = entity.ProductID);
                        new Product_Property_RRepository().Insert(entity.Product_Property_R_Extend.ToList());
                    }
                    trans.Complete();
                }
                catch (Exception)
                {

                    throw;
                }
                finally
                {
                    trans.Dispose();
                }
            }

        }

        public void Insert(IList<Product> list)
        {
            base.InsertEntity(list.Cast<Entity.EntityBase>().ToList());
        }

        public Product InsertGetIDENTITY(Product entity)
        {
            base.InsertEntity(entity);
            return this.Find(entity.PrimaryKey);
        }

        public void Delete(Product entity)
        {
            base.DeleteEntity(entity);
        }

        public void Delete(IList<Product> list)
        {
            base.DeleteEntity(list.Cast<Entity.EntityBase>().ToList());
        }

        public IQueryable<Product> GetModel()
        {
            return base.GetEntities<Product>().Select(i => new Product_Ext
            {
                CategoryID = i.CategoryID,
                CreateDate = i.CreateDate,
                Discount = i.Discount,
                Image = i.Image,
                Info = i.Info,
                MarketPrice = i.MarketPrice,
                ProductCount = i.ProductCount,
                ProductID = i.ProductID,
                ProductName = i.ProductName,
                RealPrice = i.RealPrice,
                Status = i.Status,
                UpdateDate = i.UpdateDate,
                SmallImage = i.SmallImage,
                PropertyInfo = i.PropertyInfo,
            });
        }

        public Product Find(params object[] keyValues)
        {
            return base.Find<Product>(keyValues);
        }

        #endregion
    }
}
